package com.shixianchen.review_the_day_before.day5;

import com.shixianchen.utils.Utils;
import com.shixianjun.sort.Sortable;
import com.shixianjun.sort.SortableElement;

/**
 * @program: algorithm
 * @description:
 * @author: SXC
 * @create: 2022-11-08 11:30
 **/
public class SelectSort implements Sortable {

    @Override
    public void sort(SortableElement[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            //创建索引
            int minIndex = i;
            int maxIndex = n - 1;
            for (int j = i; j < n; j++) {
                minIndex = arr[minIndex].value > arr[j].value ? j : minIndex;
                maxIndex = arr[maxIndex].value < arr[j].value ? j : maxIndex;
            }
            //交换数据
            Utils.swap(arr,minIndex,i);

            //当相遇时
            if (maxIndex == i){
                maxIndex = minIndex;
            }

            Utils.swap(arr,maxIndex,n-1);
            n = n -1;
        }

    }
}
